package com.woniuxy.micromallserver.categorymag.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.common.bean.PageDto;
import com.woniuxy.micromallserver.bean.Category;
import com.woniuxy.micromallserver.categorymag.mapper.CategoryMapper;
import com.woniuxy.micromallserver.categorymag.service.CategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author Chan
 * @Date 2021/1/21 15:23
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    public CategoryMapper categoryMapper;

    /**
     * 根据id查询分类
     *
     * @param categoryId
     * @return
     */
    @Override
    public Category getCategoryById(Integer categoryId) {
        return categoryMapper.getCategoryById(categoryId);
    }

    /**
     * 查询所有品类
     * @return
     */
    @Override
    public List<Category> getAllCategories() {
        return categoryMapper.getAllCategories();
    }

    /**
     * 多条件查询
     *
     * @return
     */
    @Override
    public List<Category> getCategoriesByMultiConditions(Category category) {
        return categoryMapper.getCategoriesByMultiConditions(category);
    }

    /**
     * 编辑分类
     * @param
     * @return
     */
    @Override
    public int updateCategory(int categoryId, String categoryName, int categoryOrder, String subcategory, String categoryDescription) {
        return categoryMapper.updateCategory(categoryId,categoryName, categoryOrder, subcategory, categoryDescription);
    }

    /**
     * 下架、删除指定分类
     * @param categoryId
     * @param categoryStatus
     * @return
     */
    @Override
    public int changeCategoryStatus(int categoryId, int categoryStatus) {
        return categoryMapper.changeCategoryStatus(categoryId, categoryStatus);
    }

    /**
     * 新增一个分类
     * @param category
     * @return
     */
    @Override
    public int addCategory(Category category) {
        return categoryMapper.addCategory(category);
    }

    /**
     * 分页查询所有品类
     *
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @Override
    public PageDto getCategoriesByPage(Integer pageNumber, Integer pageSize, Category category) {
        PageHelper.startPage(pageNumber, pageSize);
        List<Category> data = categoryMapper.getCategoriesByPage(category);
        PageInfo<Category> pageInfo = new PageInfo<>(data);

        PageDto pageDto = new PageDto();
        pageDto.setTotal(pageInfo.getTotal());
        pageDto.setData(data);

        return pageDto;
    }
}
